<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style type="text/css">
        #div1 {
            width: 198px;
            height: 66px;
            padding: 10px;
            border: 1px solid #aaaaaa;
        }

        #div2,
        #div22 {
            float: left;
            width: 198px;
            height: 66px;
            margin: 10px;
            padding: 10px;
            border: 1px solid #aaaaaa;
        }
    </style>
    <script type="text/javascript">
        function allowDrop(ev) {
            ev.preventDefault();
        }

        function drag(ev) {
            ev.dataTransfer.setData("text", ev.target.id);
        }

        function drop(ev) {
            ev.preventDefault();
            var data = ev.dataTransfer.getData("text");
            ev.target.appendChild(document.getElementById(data));
        }
    </script>
</head>

<body>
    <p>请把 W3School 的图片拖放到矩形中。默认地，数据/元素无法被放置到其他元素中。</p>

    <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <br />
    <img id="drag1" src="images/scene.jpg" draggable="true" ondragstart="drag(event)" width="100px" height="50px" />
    <hr />

    <p>来回拖放图片</p>

    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
        <img src="images/scene.jpg" draggable="true" ondragstart="drag(event)" id="drag2" width="100px" height="50px" />
    </div>
    <div id="div22" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</body>

</html>